//
//  ITSGetImageForButtonOperation.m
//  AsiaScene
//
//  Created by Rainbow on 3/18/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "ITSGetImageForButtonOperation.h"
#import "UtilHelper.h"
#import "UIHelper.h"

@implementation ITSGetImageForButtonOperation
@synthesize _imageUrl,button,_imageName;


-(id)initWithButton:(ImageButton *)abutton imageUrl:(NSString *)url nameToSave:(NSString *)name
{
	if ((self = [super init])) {
		self._imageUrl = url;
        self._imageUrl = [self._imageUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
		self._imageName = name;
		self.button = abutton;
	}
	return self;
}


-(id)initWithButton:(ImageButton *)abutton imageUrl:(NSString *)url
{
    if ((self = [super init])) {
		self._imageUrl = url;
        self._imageUrl = [self._imageUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
		self.button = abutton;
		self._imageName = nil;
	}
	return self;
}

-(void)main
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    if (self._imageUrl == nil||[@"" isEqualToString:self._imageUrl]) {
        [self.button performSelectorOnMainThread:@selector(displayImage:) withObject:nil waitUntilDone:YES];
    }
    else
    {
        UIImage *image = [UtilHelper getImageFromUrl:self._imageUrl];           
        if (image && self._imageName) {
            [UtilHelper storeImageInDocument:image ImageName:self._imageName];
        }
        [self.button performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:YES];
    }
    
	[pool release];
}

-(void)dealloc
{
    button = nil;
    _imageUrl = nil;
    _imageName = nil;
    [super dealloc];
    
}


@end
